package com.hly.musicbackground.model.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.musicbackground.Final.SongLinkFinal;
import com.hly.musicbackground.Utils.RequestContextUtils;
import com.hly.musicbackground.dao.SongLinkDao;
import com.hly.musicbackground.domain.SongLink;
import com.hly.musicbackground.model.music.VO.SongLinkVO;
import com.hly.musicbackground.model.music.params.SongLinkEmitParams;
import com.hly.musicbackground.model.music.service.SongLinkService;
import com.hly.musicbackground.model.music.service.SongSheetService;
import com.hly.musicbackground.result.Code;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SongLinkServiceImpl extends ServiceImpl<SongLinkDao, SongLink> implements SongLinkService {

    @Override
    public boolean isBoolean(String type, int link, int rid) {
        return lambdaQuery()
                .eq(SongLink::getLink, link)
                .eq(SongLink::getType, type)
                .eq(SongLink::getRid, rid)
                .one() != null;
    }

    @Override
    public List<SongLinkVO> getSongList(String type, int link, int page, int limit) {
        int userId = RequestContextUtils.getUser().getUserId();
        if (SongLinkFinal.LIKE.equals(type) && link == 0) link = userId;
        List<SongLinkVO> songLinkVOS = baseMapper.selectSongList(type, link,
                (page - 1) * limit, limit);
        return songLinkVOS;
    }

    @Override
    public void emitSong(SongLinkEmitParams params) {

        int userId = RequestContextUtils.getUser().getUserId();
        if (SongLinkFinal.LIKE.equals(params.getType()) && params.getLink() == 0) params.setLink(userId);
        if (params.getOldLinkId() != 0){
            remove(Wrappers.<SongLink>lambdaQuery()
                    .eq(SongLink::getType, SongLinkFinal.SONG_SHEET)
            .in(SongLink::getRid, params.getRid())
            .eq(SongLink::getLink, params.getOldLinkId()));
        }

        ArrayList<SongLink> songLinks = new ArrayList<>();
        for (int rid : params.getRid()){
            if (isBoolean(params.getType(), params.getLink(), rid)){
                Code.BUSINESS_ERR("已添加");
            }
            SongLink songLink = new SongLink();
            songLink.setLink(params.getLink());
            songLink.setRid(rid);
            songLink.setType(params.getType());
            songLinks.add(songLink);
        }
        saveBatch(songLinks);
    }

    @Override
    public void dels(int songSheetId, List<Integer> rid, String type) {
        remove(Wrappers.<SongLink>lambdaQuery().eq(SongLink::getLink, songSheetId)
                .eq(SongLink::getType, type).in(SongLink::getRid, rid));
    }

    @Override
    public void delAllSongLink(int id, String type) {
        remove(lambdaQuery()
                .eq(SongLink::getType, type)
                .eq(SongLink::getLink, id));
    }

    @Override
    public int countList(String type, int link) {
        List<Map<String, Object>> maps = baseMapper.selectMaps(
                new QueryWrapper<SongLink>().select("count(1) as length")
                .lambda()
                .eq(SongLink::getType, type)
                .eq(SongLink::getLink, link));
        return Integer.valueOf(String.valueOf (maps.get(0).get("length")));
    }
}
